import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

class Person{

}
class Student extends Person{

}
public class Test1 {
    public static void main(String[] args) {
        //ArrayList遍历数组
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("abc");
        arrayList.add("def");
        arrayList.add("lcc");
        arrayList.add("ssm");
        arrayList.add("ppppp");
        arrayList.add("00000000");
        //方法1
        System.out.println(arrayList);//可以直接这样输出arrayList的数组，因为ArrayList的父类重写了toString方法
        //方法2
        for (int i = 0; i < arrayList.size(); i++) {
            System.out.print(arrayList.get(i)+" ");
        }
        System.out.println();
        //方法3
        for (String x:arrayList) {
            System.out.print(x+" ");
        }
        System.out.println();
        //方法4：迭代器
        Iterator<String> it = arrayList.listIterator();
        while (it.hasNext()){
            System.out.print(it.next()+" ");
        }
        System.out.println();
    }
    public static void main4(String[] args) {
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("abc");
        arrayList.add("def");
        arrayList.add("lcc");
        arrayList.add("ssm");
        arrayList.add("ppppp");
        List<String> ret = arrayList.subList(1,3);//这里的子列表的截取并不是创建了新的对象，而是拿到了arrayList【1，3）部分的地址
        ret.set(0,"xkx");//此时改变的不仅是ret的0下标的值，同时也改变了arrayList的0下标的值
        System.out.println("======");

    }
    public static void main3(String[] args) {
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("abc");
        arrayList.add("def");
        List<String> list = new ArrayList<>();
        list.add("xyz");
        list.addAll(arrayList);
        System.out.println("======");
    }
    public static void main2(String[] args) {
        ArrayList<Person> arrayList = new ArrayList<>();
        ArrayList<Student> arrayList1 = new ArrayList<>();
        List<Person> list = new ArrayList<>(arrayList);//因为arrayList是Person类本身
        List<Person> list2 = new ArrayList<>(arrayList1);//也是可以的，因为Student是Person类的子类
    }
    public static void main1(String[] args) {
        ArrayList arrayList = new ArrayList();//不带参数的ArrayList构造方法
        ArrayList arrayList11 = new ArrayList(10);//带参数的构造方法
        ArrayList<String> arrayList2 = new ArrayList<String>();
        List<String> list = new ArrayList<>(arrayList2);//这样存放的就是String类型的数据


    }
}
